<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实现 strStr() - 字符串匹配算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-container {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0,0,0,0.15);
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #d4d4d4;
            line-height: 1.6;
        }
        .keyword { color: #c586c0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .number { color: #b5cea8; }
        .comment { color: #6a9955; }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                <i class="fas fa-code mr-4"></i>实现 strStr()
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">探索字符串匹配的艺术与算法之美</p>
            <div class="flex justify-center gap-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-clock mr-2"></i>5分钟阅读
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-chart-line mr-2"></i>算法优化
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-brain mr-2"></i>KMP算法
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-5xl">
        <!-- Problem Description Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-purple-100 p-3 rounded-full mr-4">
                    <i class="fas fa-puzzle-piece text-purple-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed">
                <span class="drop-cap">实</span>现 strStr() 函数，该函数返回子串在原串中第一次出现的位置，如果不存在则返回 -1。这是一个经典的字符串匹配问题，在文本编辑器的查找功能、搜索引擎等场景中有着广泛应用。
            </p>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-2 gap-6 mb-8">
            <div class="bg-gradient-to-br from-blue-50 to-indigo-100 rounded-2xl p-6 card-hover">
                <div class="flex items-center mb-4">
                    <i class="fas fa-lightbulb text-blue-600 text-2xl mr-3"></i>
                    <h3 class="text-xl font-bold text-gray-800">核心考点</h3>
                </div>
                <ul class="space-y-2">
                    <li class="flex items-center text-gray-700">
                        <i class="fas fa-check-circle text-green-500 mr-2"></i>
                        字符串匹配算法
                    </li>
                    <li class="flex items-center text-gray-700">
                        <i class="fas fa-check-circle text-green-500 mr-2"></i>
                        KMP算法原理
                    </li>
                    <li class="flex items-center text-gray-700">
                        <i class="fas fa-check-circle text-green-500 mr-2"></i>
                        时间复杂度优化
                    </li>
                </ul>
            </div>
            
            <div class="bg-gradient-to-br from-purple-50 to-pink-100 rounded-2xl p-6 card-hover">
                <div class="flex items-center mb-4">
                    <i class="fas fa-rocket text-purple-600 text-2xl mr-3"></i>
                    <h3 class="text-xl font-bold text-gray-800">算法应用</h3>
                </div>
                <ul class="space-y-2">
                    <li class="flex items-center text-gray-700">
                        <i class="fas fa-search text-blue-500 mr-2"></i>
                        文本搜索引擎
                    </li>
                    <li class="flex items-center text-gray-700">
                        <i class="fas fa-file-alt text-green-500 mr-2"></i>
                        文档处理系统
                    </li>
                    <li class="flex items-center text-gray-700">
                        <i class="fas fa-dna text-red-500 mr-2"></i>
                        生物信息学序列匹配
                    </li>
                </ul>
            </div>
        </div>

        <!-- Algorithm Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold text-gray-800 mb-6 text-center">
                <i class="fas fa-sitemap mr-3 text-gradient"></i>算法思路可视化
            </h2>
            <div class="mermaid">
                graph TD
                    A[开始匹配] --> B{子串是否为空?}
                    B -->|是| C[返回 0]
                    B -->|否| D[初始化指针 i=0]
                    D --> E{i <= 主串长度-子串长度?}
                    E -->|否| F[返回 -1]
                    E -->|是| G[从位置 i 开始匹配]
                    G --> H{子串完全匹配?}
                    H -->|是| I[返回位置 i]
                    H -->|否| J[i++]
                    J --> E
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style I fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style F fill:#f56565,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Solution Approach -->
        <div class="bg-gradient-to-r from-gray-50 to-gray-100 rounded-2xl p-8 mb-8">
            <h2 class="text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-route mr-3 text-indigo-600"></i>解题思路
            </h2>
            
            <div class="space-y-6">
                <div class="bg-white rounded-xl p-6 shadow-md card-hover">
                    <h3 class="text-xl font-semibold text-gray-800 mb-3 flex items-center">
                        <span class="bg-indigo-100 text-indigo-600 px-3 py-1 rounded-full text-sm mr-3">方法一</span>
                        暴力匹配算法
                    </h3>
                    <p class="text-gray-700 mb-4">
                        最直观的方法是在主串中逐个位置尝试匹配子串。从主串的每个位置开始，逐字符比较是否与子串匹配。
                    </p>
                    <div class="bg-gray-50 rounded-lg p-4">
                        <div class="flex items-center justify-between text-sm">
                            <span class="text-gray-600">
                                <i class="fas fa-clock mr-2 text-blue-500"></i>
                                时间复杂度：O(m × n)
                            </span>
                            <span class="text-gray-600">
                                <i class="fas fa-memory mr-2 text-green-500"></i>
                                空间复杂度：O(1)
                            </span>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-xl p-6 shadow-md card-hover opacity-75">
                    <h3 class="text-xl font-semibold text-gray-800 mb-3 flex items-center">
                        <span class="bg-purple-100 text-purple-600 px-3 py-1 rounded-full text-sm mr-3">方法二</span>
                        KMP算法 <span class="text-sm text-gray-500 ml-2">(进阶)</span>
                    </h3>
                    <p class="text-gray-700 mb-4">
                        利用已匹配的信息，避免重复比较。通过构建部分匹配表（next数组），实现高效的字符串匹配。
                    </p>
                    <div class="bg-gray-50 rounded-lg p-4">
                        <div class="flex items-center justify-between text-sm">
                            <span class="text-gray-600">
                                <i class="fas fa-clock mr-2 text-blue-500"></i>
                                时间复杂度：O(m + n)
                            </span>
                            <span class="text-gray-600">
                                <i class="fas fa-memory mr-2 text-green-500"></i>
                                空间复杂度：O(m)
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="mb-8">
            <h2 class="text-3xl font-bold text-gray-800 mb-6">
                <i class="fas fa-code mr-3 text-green-600"></i>示例代码
            </h2>
            
            <div class="code-container">
                <div class="code-header">
                    <div class="code-dot bg-red-500"></div>
                    <div class="code-dot bg-yellow-500"></div>
                    <div class="code-dot bg-green-500"></div>
                    <span class="text-gray-400 ml-4 text-sm">solution.py</span>
                </div>
                <pre><code><span class="keyword">def</span> <span class="function">strStr</span>(haystack, needle):
    <span class="keyword">if</span> <span class="keyword">not</span> needle:
        <span class="keyword">return</span> <span class="number">0</span>
    
    <span class="comment"># 遍历主串中所有可能的起始位置</span>
    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="function">range</span>(<span class="function">len</span>(haystack) - <span class="function">len</span>(needle) + <span class="number">1</span>):
        <span class="comment"># 检查从位置 i 开始的子串是否匹配</span>
        <span class="keyword">if</span> haystack[i:i+<span class="function">len</span>(needle)] == needle:
            <span class="keyword">return</span> i
    
    <span class="keyword">return</span> -<span class="number">1</span></code></pre>
            </div>
        </div>

        <!-- Tips and Tricks -->
        <div class="bg-gradient-to-br from-yellow-50 to-orange-100 rounded-2xl p-8">
            <h2 class="text-2xl font-bold text-gray-800 